package com.hpjz.basicofing.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.hpjz.basicofing.bean.CarBean;
import com.hpjz.basicofing.bean.OrgRCarBean;
import com.hpjz.basicofing.bean.OrganizationBean;
import com.hpjz.basicofing.dao.ICarDao;
import com.hpjz.basicofing.dao.IOrgRCarDao;
import com.hpjz.basicofing.dao.IOrganizationDao;
import com.hpjz.basicofing.service.ICarService;
import com.hpjz.basicofing.web.vo.CarVO;
import com.hpjz.basicofing.web.vo.Position;
import com.hpjz.basicofing.web.vo.RunCarVO;
import com.hpjz.common.utils.DateUtil;
import com.hpjz.emrecord.bean.EmRecordBean;
import com.hpjz.emrecord.bean.GpsInfoBean;
import com.hpjz.emrecord.bean.PositionBean;
import com.hpjz.emrecord.bean.RunCarBean;
import com.hpjz.emrecord.dao.EmRecordDao;
import com.hpjz.emrecord.dao.GpsInfoDao;
import com.hpjz.emrecord.web.vo.EmRecordVO;
import com.hpjz.emrecord.web.vo.GpsInfoVO;

@Service("carService")
public class CarServiceImpl implements ICarService{
	
	@Resource
	private ICarDao carDao;
	
	@Resource
	private IOrgRCarDao orgRCarDao;
	
	@Resource
	private IOrganizationDao organizationDao;
	
	@Resource
	private EmRecordDao emRecordDao;
	
	@Resource
	private GpsInfoDao gpsInfoDao;

	@Override
	public boolean insertCar(CarVO vo) {
		//是否重复
		boolean isRepeat = false;
		CarBean bean = this.voToBean(vo);
		bean.setCreatetime(new Date());
		bean.setUpdatetime(new Date());
		//对车辆做查重处理
		CarBean sbean = new CarBean();
		sbean.setCarlicensplate(bean.getCarlicensplate());
		CarBean rbean = this.carDao.findOne(sbean);
		if (rbean!=null) {
			isRepeat = true;
		}else{
			//添加车辆信息
			bean.setStatus("1");
			this.carDao.insert(bean);
		}
		
		return isRepeat;
	    
	   
        //如果选择了机构添加机构（暂时不做关联）
		/*OrgRCarBean orgRcar = new OrgRCarBean();
		String orgId = vo.getOrg_id();
		if (StringUtils.isNotBlank(orgId)) {
		   orgRcar.setOrg_id(Long.valueOf(orgId));
		   orgRcar.setCar_id(bean.getId());
		   this.orgRCarDao.insert(orgRcar);
		}*/
	   
	}
	

	@Override
	public void updateCar(CarVO vo) {
		CarBean bean = this.voToBean(vo);
		bean.setUpdatetime(new Date());
		this.carDao.update(bean);
		//同时修改机构
		/*String orgId = vo.getOrg_id();
		OrgRCarBean orbean = new OrgRCarBean();
 	    orbean.setCar_id(bean.getId());
        orbean.setOrg_id(Long.valueOf(orgId));
        this.orgRCarDao.update(orbean);*/
	}

	@Override
	public void deleteCar(CarVO vo) {
		CarBean bean = this.voToBean(vo);
		this.carDao.delete(bean);
		//删除对应的结构
		/*OrgRCarBean orbean = new OrgRCarBean();
		orbean.setCar_id(bean.getId());
		this.orgRCarDao.delete(orbean);*/
	}

	@Override
	public CarVO findCar(CarVO vo) {
		CarBean bean = this.voToBean(vo);
		CarBean resultBean = this.carDao.findOne(bean);
		
		CarVO result =  this.beanToVO(resultBean);
		//查询车辆所在的机构
	   /* OrgRCarBean orbean = new OrgRCarBean();
	    OrgRCarBean orgbean = this.orgRCarDao.findOne(orbean);
	    OrganizationBean orgabean = new OrganizationBean();
	    orgabean.setOr_id(orgbean.getOrg_id());
		OrganizationBean orgBean = this.organizationDao.findOne(orgabean);
		//设置返回模型
		result.setOrg_name(orgBean.getOr_name()); 
		result.setOrg_id(String.valueOf(orgBean.getOr_id()));*/
		
		return result;
	}

    /**
     *  将bean转化为vo
     * @param bean
     * @return
     */
	private CarVO beanToVO(CarBean bean) {
		CarVO result = new CarVO();
		BeanUtils.copyProperties(bean, result);
		Long id = bean.getCarid();
		Date creatTime = bean.getCreatetime();
		Date updateTime = bean.getUpdatetime();
		if (id!=null) {
			result.setCarid(String.valueOf(id));
		}
		if (creatTime!=null) {
			result.setCreatetime(DateUtil.dateToStr(creatTime));
		}
		if (updateTime!=null) {
			result.setUpdatetime(DateUtil.dateToStr(updateTime));
		}
		return result;
	}
	 
	/**
	 * 将vo转换为bean
	 * @param vo
	 * @return
	 */
	private CarBean voToBean(CarVO vo) {
		 CarBean bean = new CarBean();
		 BeanUtils.copyProperties(vo, bean);
		 String id = vo.getCarid();
		 if (StringUtils.isNotBlank(id)) {
			bean.setCarid(Long.valueOf(id));
		 }
		
		return bean;
	}


	@Override
	public List<CarVO> findAllCars() {
		List<CarBean> carList = this.carDao.findAll();
		List<CarVO> vos = beansToVos(carList);
		return vos;
	}

    /**
     * 将beanList转化为voList
     * @param carList
     * @return
     */
	private List<CarVO> beansToVos(List<CarBean> carList) {
		List<CarVO> vos = new ArrayList<>();
		for (CarBean car : carList) {
			CarVO vo = this.beanToVO(car);
			vos.add(vo);
		}
		return vos;
	}


	@Override
	public List<RunCarVO> findRunCars() {
		
		List<RunCarBean>  gpsInfos = this.gpsInfoDao.findAllRunCars();
		
		List<RunCarVO> vos = new ArrayList<>();
		
		for (RunCarBean rb : gpsInfos) {
			RunCarVO vo = new RunCarVO();
			vo.setRecordid(String.valueOf(rb.getRecordid()));
			BeanUtils.copyProperties(rb, vo);
			List<PositionBean> pbs = rb.getPosition();
			/**
			 * 对位置信息按时间排序
			 */
			Collections.sort(pbs, new Comparator<PositionBean>() {
				@Override
				public int compare(PositionBean p1, PositionBean p2) {
					Long time1 = p1.getLasttime().getTime();
					Long time2 = p2.getLasttime().getTime();
					return time2.compareTo(time1);
				}
			});
			
			
			List<Position> pos = new ArrayList<>();
			for (PositionBean p : pbs) {
				Position pn = new Position();
				BeanUtils.copyProperties(p, pn);
				pn.setLasttime(DateUtil.dateToStr(p.getLasttime()));
				pos.add(pn);
			}
			vo.setPosition(pos);
			vos.add(vo);
		}
		
		/*List<RunCarVO> vos = new ArrayList<>();
		for (GpsInfoBean rb : gpsInfos) {
			RunCarVO vo = new RunCarVO();
			vo.setRecordid(String.valueOf(rb.getRecordid()));
			List<Position> p = new ArrayList<>();
			vo.setPosition(p);
	
			
			BeanUtils.copyProperties(rb, vo);
			vos.add(vo);
		}*/
		return vos;
	}
	
	
	

}
